﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QLK.Web.App_Code;
using QLK.Database.BLLs;
using QLK.Database;
using Utilities;

namespace QLK.Web
{
    public partial class ImportInvoicesDetails : System.Web.UI.Page
    {
        private string _importID;
        SupplierBLL suppBLL = new SupplierBLL();
        StorageBLL storageBLL = new StorageBLL();
        EmployeeBLL employeeBLL = new EmployeeBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            _importID = Request.QueryString[Columns.ImportID];
            if (!IsPostBack)
            {
                if (_importID != null && _importID != string.Empty)
                {
                    int ID = ConvertHelper.CheckQueryString_Int(_importID);
                    if (ID == -1)
                    {
                        //redirect to error page
                    }
                    else
                    {

                    }
                }

                LoadSupplierID();
                LoadStorageID();
                LoadEmployee();
                LoadSupplierID();
                //LoadImportInvoicesDetails(ID);
            }
        }
        private void LoadSupplierID()
        {
        DropSupplierID.DataSource = suppBLL.GetAllSupplier();
        DropSupplierID.DataTextField = "SupplierName";
        DropSupplierID.DataValueField = "SupplierID";
        DropSupplierID.DataBind();
        }
        private void LoadStorageID()
        {
            DropStorageID.DataSource = storageBLL.StorageGetAll();
            DropStorageID.DataTextField = "StoregeName";
            DropStorageID.DataValueField = "StorageID";
            DropStorageID.DataBind();
        }
        private void LoadEmployee()
        {
            DropEmployeeID.DataSource = employeeBLL.EmployeeGetAll();
            DropEmployeeID.DataTextField = "LastName";
            DropEmployeeID.DataValueField = "EmployeeID";
            DropEmployeeID.DataBind();
        }
        private void LoadImportInvoicesDetails(int ID)
        {
            ImportInvoiceBLL bll = new ImportInvoiceBLL();
            ImportInvoice import;
            import = bll.GetImportInvoiceByImportID(int.Parse(_importID));
            txtImportDate.Text =Request.QueryString[Columns.ImportDate];
            txtTotalValues.Text =Request.QueryString[Columns.TotalValues];
            DropInvoiceID.Text = Request.QueryString[Columns.InvoiceID];
            DropStorageID.Text=Request.QueryString[Columns.StorageID];
            DropSupplierID.Text=Request.QueryString[Columns.SupplierID];
            DropEmployeeID.Text=Request.QueryString[Columns.EmployeeID];
            
            // gan vao text box
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool res = false;
            ImportInvoiceBLL bll = new ImportInvoiceBLL();
            ImportInvoice import;
            if (_importID != null && _importID != string.Empty)
            {
                import = bll.GetImportInvoiceByImportID(int.Parse(_importID));
                //import.InvoceID = DropInvoiceID.SelectedValue.ToString();
                import.StorageID = int.Parse(DropStorageID.SelectedValue.ToString());
                import.ImportDate = DateTime.Now;
                import.SupplierID = int.Parse(DropSupplierID.SelectedValue.ToString());
                import.EmployeeID = int.Parse(DropEmployeeID.SelectedValue.ToString());
                import.TotalValues = int.Parse(txtTotalValues.Text.Trim());

                if (bll.UpdateImportInvoice(import))
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "update successfully!";
                    res = true;
                }

            }
            else
            {
                import = new ImportInvoice();
                import.InvoceID = DropInvoiceID.SelectedValue.ToString();
                //import.StorageID = int.Parse(DropStorageID.SelectedValue.ToString());
                import.ImportDate = DateTime.Parse(txtImportDate.Text);
                import.SupplierID = int.Parse(DropSupplierID.SelectedValue.ToString());
                import.EmployeeID = int.Parse(DropEmployeeID.SelectedValue.ToString());
                import.TotalValues = int.Parse(txtTotalValues.Text.Trim());
                if (bll.InsertImportInvoice(import))
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "insert successfully!";
                    Response.Redirect(PageUrls.ImportInvoiceManagement);
                    res = true;
                }
            }
            if (res)
                Response.Redirect(PageUrls.ImportInvoiceManagement);
            else
            {
                // loi
            }
        }
    }
}
